package leetcode;

class Solution_647 {

    public int countSubstrings(String s) {
        int count = 0;
        boolean[][] res = new boolean[s.length()][s.length()];
        for (int i = 0; i < s.length(); i++) {
            res[i][i]= true;
        }
        for (int start = s.length() -1; start >= 0; start--) {
            for (int end = start; end < s.length(); end++) {
                if (s.charAt(start) == s.charAt(end)){
                    if (end - start <= 1){
                        res[start][end] = true;
                        count ++;
                    }else {
                        if (res[start + 1][end -1]) {
                            res[start][end] = true;
                            count ++;
                        }else {
                            res[start][end] = false;
                        }
                    }
                }else {
                    res[start][end] = false;
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(new Solution_647().countSubstrings("aaa"));
    }
}